﻿using UnityEngine;

public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T> {
    private static T _instance;

    public static T Instance {
        get {
            if (_instance == null) {
                GameObject go = new GameObject(typeof(T).Name);
                _instance = go.AddComponent<T>();
                _instance.Init();
            }
            return _instance;
        }
    }

    private void Awake() {
        if (_instance != null && _instance != this) {
            Destroy(gameObject);
        }
        _instance = this as T;
        DontDestroyOnLoad(gameObject);
    }
    public virtual void Init() { }
}
public class MonoSingleton_Common<T> : MonoBehaviour where T : MonoSingleton_Common<T> {
    private static T _instance;

    public static T Instance {
        get {
            if (_instance == null) {
                GameObject go = new GameObject(typeof(T).Name);
                _instance = go.AddComponent<T>();
            }
            return _instance;
        }
    }

    private void Awake() {
        if (_instance != null && _instance != this) {
            Destroy(gameObject);
        }
        _instance = this as T;
        OnAwake();
    }

    protected virtual void OnAwake() { }
}

public class MonoSingleton_Low<T> : MonoBehaviour where T : MonoSingleton_Low<T> {
    private static T _instance;
    public static T Instance {
        get {
            return _instance;
        }
    }
    private void Awake() {
        if (_instance != null && _instance != this) {
            Destroy(gameObject);
        }
        _instance = this as T;
    }
}